import 'dart:async';

import 'package:flutter/material.dart';

class LocalStreamController {
  StreamController<LocalSEvent> _streamController = StreamController.broadcast();

  ValueChanged<LocalSEvent>? _callback;

  LocalStreamController(){
    _streamController.stream.listen((event) {
      if(!_streamController.isClosed){
        if(_callback != null){
          _callback!(event);
        }
      }
    });
  }

  void dispose(){
    _streamController.close();
  }

  void add(LocalSEvent event){
    if(!_streamController.isClosed){
      _streamController.add(event);
    }
  }

  void listener(ValueChanged<LocalSEvent> f){
    this._callback = f;
  }
}


enum LocalSEvent {
  Begin,
  Pause,
  Stop,
  SwitchCar,
  StartRecord,
  StopRecord,
  OpenMicrophone,
  OffMicrophone
}